<?php

namespace app\admin\controller\adv;

use app\common\controller\Backend;
use think\helper\Time;

/**
 * 链接列管理
 *
 * @icon fa fa-circle-o
 */
class Lists extends Backend
{

    /**
     * Lists模型对象
     * @var \app\admin\model\adv\Lists
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\adv\Lists;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->field(['id', 'name', 'link','memo', 'interval', 'target', 'pv', 'uv', 'status', 'createtime', 'desc'])
                ->with(['html'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit)
                ->toArray();

            list($ts, $te) = Time::today();
            foreach ($list['data'] as &$row) {
                $row['desc'] = mb_substr($row['desc'],0,10)."...";
                $row['link'] = $this->request->domain() . "/ads/link/" . $row['link'] . ".html";
                $row['today_pv'] = $this->model->getCount($row['id'], 'pv', $ts, $te);
                $row['today_uv'] = $this->model->getCount($row['id'], 'uv', $ts, $te);
            }
            $result = array("total" => $list['total'], "rows" => $list['data']);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $row = $this->request->post('row/a');
            foreach ($row as $k => $v) {
                if ($k == 'name' || $k == 'link') {
                    $tips = $k == 'name' ? '名称':'自定义链接';
                    if (trim($v) == '') $this->error("请输入{$tips}");
                    else {
                        if ($this->model->where([$k => $v])->find()) $this->error("{$tips}已存在");
                    }
                }
            }
        }
        return parent::add();
    }

    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $row = $this->request->post('row/a');
            foreach ($row as $k => $v) {
                if ($k == 'name' || $k == 'link') {
                    $tips = $k == 'name' ? '名称':'自定义链接';
                    if (trim($v) == '') $this->error("请输入{$tips}");
                    else {
                        if ($this->model->where(['id' => ['<>', $ids], $k => $v])->find()) $this->error("{$tips}已存在");
                    }
                }
            }
        }
        return parent::edit($ids);
    }

    public function check($ids = '')
    {
        if ($this->request->isPost()) {
            $row = $this->request->post('row/a');
            if (isset($row['name'])) {
                $res = checkFieldNullRepeat('name', trim($row['name']),$ids,$this->model);
                if ($res == 'null') $this->error("名称不能为空");
                if ($res)  $this->error('名称已存在');
            }
            if (isset($row['link'])) {
                $res = checkFieldNullRepeat('link', trim($row['link']),$ids,$this->model);
                if ($res == 'null') $this->error("自定义链接不能为空");
                if ($res)  $this->error('自定义链接已存在');
            }
            $this->success();
        }
    }

}
